<template>
  <div class="login-container">
    <el-card class="login-box">
      <h2 class="title">X-PRO</h2>
      <el-form :model="form" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            @keyup.enter="handleLogin(loginFormRef)"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            @keyup.enter="handleLogin(loginFormRef)"
          />
        </el-form-item>

        <div class="remember-forgot">
          <el-checkbox v-model="form.remember">记住我</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码？</el-link>
        </div>

        <el-button
          type="primary"
          class="login-btn"
          @click="handleLogin(loginFormRef)"
          :loading="loading"
        >
          登录
        </el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const form = reactive({
  username: '',
  password: '',
  remember: false,
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const loginFormRef = ref()

const handleLogin = async (loginFormRef: FormInstance) => {
  await loginFormRef.validate(async (valid: boolean) => {
    if (valid) {
      localStorage.setItem('token', 'asd')
      // 处理登录逻辑
      loading.value = true
      try {
        // await loginAPI(form)
        await router.push('/')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/bg_image.jpg');
  background-size: cover;
}

.login-box {
  width: 400px;
  padding: 30px;
}

.title {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
}
</style>
